package com.wskh.msmService.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.wskh.msmService.helper.SpringHelper;
import com.wskh.msmService.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Map;

@Component
public class MsmServiceImpl implements MsmService {

    @Override
    public boolean sendPhone(String phone, String sms_180051135, Map<String, Object> param) {

        if(StringUtils.isEmpty(phone)){
            return false;
        }

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI5tQjTn7eF4ojoXeu5MS2",
                        "XguaeQxFIFHMOFutohfaneloGZ18Wo");

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
        request.putQueryParameter("TemplateCode", sms_180051135);
        request.putQueryParameter("TemplateParam",
                JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;

    }

    @Override
    public boolean sendEmail(String email,String code) {

        try {
            JavaMailSenderImpl javaMailSender = SpringHelper.getBean(JavaMailSenderImpl.class);

            MimeMessage mimeMessage = javaMailSender.createMimeMessage();

            // 组装
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");

            // 主题
            helper.setSubject("主题:小白学院注册验证码");
            helper.setText("您的注册验证码为(5分钟内有效):"+code);
            helper.setFrom("1187560563@qq.com");
            helper.setTo(email);
            javaMailSender.send(mimeMessage);

            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
